

* 1) creates COW variable
* 2) creates a dataset with gvkey, year, and state
* 3) creates a COW dummy
* 4) creates COW treatment at the TNIC4 level.

cd "`c(pwd)'"   // Set working directory to wherever the user placed "replication_package"

* Define global paths relative to the main directory
global main_dir "`c(pwd)'"
global data_dir "$main_dir/data_analysis"
global rawdata_dir "$main_dir/data_raw"
global tables_dir "$main_dir/tables"

cd "$data_dir/"
use sample_od, clear
cd "$rawdata_dir/"
joinby gvkey year using crsp_compustat_1990_2020, unmatched(master)
keep gvkey year incorp
rename incorp state_code
gen state=.
{
replace state = 1 if state_code == "AL"
replace state = 2 if state_code == "AK"
replace state = 4 if state_code == "AZ"
replace state = 5 if state_code == "AR"
replace state = 6 if state_code == "CA"
replace state = 8 if state_code == "CO"
replace state = 9 if state_code == "CT"
replace state = 10 if state_code == "DE"
replace state = 11 if state_code == "DC"
replace state = 12 if state_code == "FL"
replace state = 13 if state_code == "GA"
replace state = 15 if state_code == "HI"
replace state = 16 if state_code == "ID"
replace state = 17 if state_code == "IL"
replace state = 18 if state_code == "IN"
replace state = 19 if state_code == "IA"
replace state = 20 if state_code == "KS"
replace state = 21 if state_code == "KY"
replace state = 22 if state_code == "LA"
replace state = 23 if state_code == "ME"
replace state = 24 if state_code == "MD"
replace state = 25 if state_code == "MA"
replace state = 26 if state_code == "MI"
replace state = 27 if state_code == "MN"
replace state = 28 if state_code == "MS"
replace state = 29 if state_code == "MO"
replace state = 30 if state_code == "MT"
replace state = 31 if state_code == "NE"
replace state = 32 if state_code == "NV"
replace state = 33 if state_code == "NH"
replace state = 34 if state_code == "NJ"
replace state = 35 if state_code == "NM"
replace state = 36 if state_code == "NY"
replace state = 37 if state_code == "NC"
replace state = 38 if state_code == "ND"
replace state = 39 if state_code == "OH"
replace state = 40 if state_code == "OK"
replace state = 41 if state_code == "OR"
replace state = 42 if state_code == "PA"
replace state = 44 if state_code == "RI"
replace state = 45 if state_code == "SC"
replace state = 46 if state_code == "SD"
replace state = 47 if state_code == "TN"
replace state = 48 if state_code == "TX"
replace state = 49 if state_code == "UT"
replace state = 50 if state_code == "VT"
replace state = 51 if state_code == "VA"
replace state = 53 if state_code == "WA"
replace state = 54 if state_code == "WV"
replace state = 55 if state_code == "WI"
replace state = 56 if state_code == "WY"
}

gen COW=0 if state!=.
replace COW=1 if state==10 & year>=2000
replace COW=1 if state==40 & year>=2002
replace COW=1 if state==29 & year>=2004
replace COW=1 if state==20 & year>=2005
replace COW=1 if state==48 & year>=2006
replace COW=1 if state==32 & year>=2008
replace COW=1 if state==34 & year>=2011
replace COW=1 if state==24 & year>=2015
replace COW=1 if state==53 & year>=2016

keep gvkey year COW
drop if gvkey==gvkey[_n-1] & year==year[_n-1]
cd "$data_dir/"
save bygvkey_COW, replace
replace year=year+1
rename COW COWl1
save bygvkey_COWl1, replace

* % of TNIC4 peers that are affected by COWs
use tnic4_data, clear
drop if gvkey1==gvkey2
rename gvkey1 gvkey
joinby gvkey year using sample_od, unmatched(master)
drop if _merge!=3
drop _merge
rename gvkey gvkey1
rename gvkey2 gvkey
joinby gvkey year using sample_od, unmatched(master)
drop if _merge!=3
drop _merge
joinby gvkey year using bygvkey_COW, unmatched(master)
drop _merge
rename gvkey gvkey2
keep gvkey1 gvkey2 year COW
bys gvkey1 year: egen COWtn4=mean(COW)
rename gvkey1 gvkey
drop gvkey2
collapse (mean)  COWtn4 , by(year gvkey)
save avgCOWtn4, replace


